package com.riven.bilibilidemo.utils;


import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class HttpUtilsOne {


    private HttpUtilsOne(){}

    /**
     * 静态内部类
     */
    private static class HttpUtilsClass{
        private static final HttpUtilsOne HTTP_UTILS = new HttpUtilsOne();
    }

    /**
     * 单例模式
     * @return 返回本对象
     */
    public static HttpUtilsOne getInstanc(){
        return HttpUtilsClass.HTTP_UTILS;
    }

    /**
     * 获得ApiServer
     * @param clazz Retrofit的Api接口
     * @param baseUrl 主地址
     * @param <T>
     * @return Api接口对象
     */
    public static <T> T getAPIServer(Class<T> clazz, String baseUrl){
        Retrofit retrofit = new Retrofit
                .Builder()
                .baseUrl(baseUrl)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        return retrofit.create(clazz);

    }

    /**
     *
     * @param observable 被观察者
     * @param observer 观察者
     * @param <T>
     */
    public static <T> void getObServer(Observable<T> observable , Observer<T> observer){
                observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
}
